<?php
namespace Home\Controller;

use Home\Model\ApplicationFormModel;
use Home\Model\UseReturnModel;

class MyApprovalController extends CommonController{
  
  protected $table="application_form";
  protected $pageSize = 12;
  
  public function index(){
		$this->assign('appType', $this->M->getType());
		$this->assign('departmentType', $this->M->getDepartmentType());
		$this->assign('monthType', $this->M->getMonthType());
		$this->assign('appStatus', $this->M->getAppStatus());
		$this->assign("userNames", D("ApplicationForm")->getUserNames());
  	$this->display();
  }

	public function getList(){
		$this->setQeuryCondition();
    $count = (int)$this->M->where(array('approver'=>$_SESSION['account']['userInfo']['user_id']))->count();
    $this->setQeuryCondition();
    $list = $this->M->page(I('get.p',0). ','. $this->pageSize)->where(array('approver'=>$_SESSION['account']['userInfo']['user_id']))->order('id desc')->select();
    foreach($list as $k=>&$v){
	    	$id = $v['app_name'];
	        if($v['type']!=5 ){
	        	$re = D('user_info')->where(array('user_id'=>$id))->field('realname')->find();
				$v['app_name'] = $re['realname'];
	        }
	    }
	  foreach($list as $k=>&$v){
			$re = $v['send_copy'];
			$res=explode(',',$re);
			$res1 = array();
			for($i=0;$i<count($res);$i++){
				$res1[] = D('user_info')->where(array('user_id'=>$res[$i]))->field('realname')->find();
			}
			
			
			$send_copy = "";
			$counts = count($res1);
			for($j = 0; $j < $counts; $j++){
			  $send_copy .= $res1[$j]['realname'].',';
			}
			$send_copy = substr($send_copy,0,strlen($send_copy)-1); 
			$v['send_copy'] = $send_copy;
		}
    $result = array('list'=>$list, 'count'=>$count);
		$this->ajaxReturn($result);

	}

	//设置查询
  public function setQeuryCondition(){

	if (I('get.type')) {
		$this->M->where(array("type"=> array('EQ', I('get.type'))));
	}
	
	if(I('get.app_status')){
		$this->M->where(array("app_status"=> array('EQ', I('get.app_status'))));
	}
	
	if (I('get.start1') && I('get.start2')) {
		$start1 = I('get.start1');
		$start2 = I('get.start2');
		$d = date('Y-m-d',strtotime($start2)+86400);
		$this->M->where(array("app_time"=> array('between', "$start1,$d")));
	}
	
	if (I('get.end1') && I('get.end2')) {
		$end1 = I('get.end1');
		$end2 = I('get.end2');
		$d = date('Y-m-d',strtotime($end2)+86400);
		$this->M->where(array("exp_time"=> array('between', "$end1,$d")));
	}
		
    $this->M->where( array('info_status'=>array('EQ', 1),'check_status'=>array('EQ',1) ) );

  }
	
	public function approval(){
		$state = D('ApplicationForm')->where(array('id'=>array('EQ',$_POST['id'])))->getField('approval_time');
		if($state){
			$this->error('该条信息已审批，不可再次审批！');
		}
		$_POST['approval_time'] = date('Y-m-d H:i:s',time());
		$_POST['app_process'] = 2;
		$_POST['belongs_department'] = D('ApplicationForm')->getSelfDepartment();
		$re = $this->M->create($_POST, 2);
		if($re){
			if($_POST['app_status'] == '1'){//审批通过
				$this->M->startTrans(); //开启事务
				//查询当前申请的详细信息
				$result = array();
				$id = $_POST['id'];
				$list = D('application_form')->where(array('id'=>array('in', $id)))->select();
				$result['name'] = $list[0]['name'];
				$result['unit'] = $list[0]['unit'];
				$result['specification'] = $list[0]['specification'];
				$result['belongs_department'] = $list[0]['belongs_department'];
				$app_type = $list[0]['type'];
				//查询领用表是否有当前用品信息
				$res = D('useReturn')->where(array('name'=>$result['name'],'unit'=>$result['unit'],'specification'=>$result['specification'],'belongs_department'=>$result['belongs_department']))->select();
				if(!$res){
					$this->M -> save();
					$this->M->commit();
					$this->success(L('ADD_SUCCESS'));
				}else{//如果有，再去库存表查询已经报修报废的数量
					$relt = D('inventoryInformation')->where(array('name'=>$result['name'],'unit'=>$result['unit'],'specification'=>$result['specification'],'belongs_department'=>$result['belongs_department']))->select();
					if($app_type == '2'){//报修
						$result['repair_num'] = $relt[0]['repair_num'] + $list[0]['num'];
						$result['using_num']  = $relt[0]['using_num']  - $list[0]['num'];
						if (M('inventoryInformation')->where(array('name'=>$result['name'],'unit'=>$result['unit'],'specification'=>$result['specification'],'belongs_department'=>$result['belongs_department']))->save($result) !== false) {
							$this->M -> save();
							$this->M->commit();
							$this->success(L('ADD_SUCCESS'));
						}else{
							$this->M->rollback();
							$this->error($this->M->getError().$this->M->getLastSql());
						}
					}else if($app_type == '3'){//报废
						$result['scrap_num'] = $relt[0]['scrap_num'] + $list[0]['num'];
						$result['using_num'] = $relt[0]['using_num'] - $list[0]['num'];
						if (M('inventoryInformation')->where(array('name'=>$result['name'],'unit'=>$result['unit'],'specification'=>$result['specification'],'belongs_department'=>$result['belongs_department']))->save($result) !== false) {
							$this->M -> save();
							$this->M->commit();
							$this->success(L('ADD_SUCCESS'));
						}else{
							$this->M->rollback();
							$this->error($this->M->getError().$this->M->getLastSql());
						}
					}else{
						$this->M -> save();
					  $this->M->commit();
					  $this->success(L('ADD_SUCCESS'));
					}

				}
			}else{//审批不通过
				if ($this->M->save() !== false) {
					$this->success(L('ADD_SUCCESS'));
				} else {
					$this->error($this->M->getError().$this->M->getLastSql());
				}
			}
		}else{
			$this->error($this->M->getError().$this->M->getLastSql());
		}
	}









}